﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Windows.Forms;

public partial class order_infor : System.Web.UI.Page
{
    BLL.Business.OrderBLL orderBLL = new BLL.Business.OrderBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
    }

    // 绑定
    public void BindData()
    {
        try
        {
            if (!object.Equals(Request.QueryString["IsUser"], null)) // 判断是会员还是管理员查看订单
            {
                this.submitSend.Visible = false;
                this.submitGetMoney.Visible = false;
            }
            if (!object.Equals(Request.QueryString["Ord_Idint"], null))
            {
                String orderId = Request.QueryString["Ord_Idint"];
                DataSet ds_Order = new DataSet();                    // 订单基本信息

                ds_Order = orderBLL.SelectByID(int.Parse(orderId));  // 订单基本信息
                if ((ds_Order == null) ||
                   (ds_Order.Tables.Count == 0) ||
                   (ds_Order.Tables.Count == 1 && ds_Order.Tables[0].Rows.Count == 0))
                {
                    Response.Write("<script>alert('没找到订单！')</script>");
                }
                else
                {
                    double totalYuan = 0;
                    int orderChecked = 0;              // 订单状态
                    grdOrderList.DataSource = ds_Order;// 绑定gridview
                    grdOrderList.DataBind();
                    foreach (DataRow dr in ds_Order.Tables[0].Rows)
                    {
                        String cost = dr["Detail_TotalCostflo"].ToString();
                        totalYuan += Convert.ToDouble(cost);
                    }
                    lblSum.Text = totalYuan.ToString();            //总花费 

                    orderChecked = Int32.Parse(ds_Order.Tables[0].Rows[0]["Ord_IsCheckedint"].ToString());  // 获得状态
                    if (orderChecked == 0)     // 没发货
                    { }
                    else if (orderChecked == 1)// 已发货
                    { this.submitSend.Enabled = false; }
                    else                       // 已结账
                    {
                        this.submitSend.Enabled = false;
                        this.submitGetMoney.Enabled = false;
                    }
                    txtAddress.Text = ds_Order.Tables[0].Rows[0]["Ord_Addressstr"].ToString();
                    txtEmail.Text = ds_Order.Tables[0].Rows[0]["Ord_Emailstr"].ToString();
                    txtFuKuanFangShi.Text = ds_Order.Tables[0].Rows[0]["Ord_Paymentstr"].ToString();
                    txtPhone.Text = ds_Order.Tables[0].Rows[0]["Ord_Telephonestr"].ToString();
                    txtPostalCode.Text = ds_Order.Tables[0].Rows[0]["Ord_PostalCodestr"].ToString();
                    txtRemark.Text = ds_Order.Tables[0].Rows[0]["Ord_Remarkstr"].ToString();
                    txtSongHuoFangShi.Text = ds_Order.Tables[0].Rows[0]["Ord_Deliverystr"].ToString();
                    txtLinkMan.Text = ds_Order.Tables[0].Rows[0]["Ord_ContactManstr"].ToString();
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }

    // 发货
    protected void submitSend_Click(object sender, EventArgs e)
    {
        try
        {
            String orderId = Request.QueryString["Ord_Idint"];
            orderBLL.UpdateOrderChecked(Int32.Parse(orderId), 1);
            BindData();
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
    // 收款
    protected void submitGetMoney_Click(object sender, EventArgs e)
    {
        try
        {
            String orderId = Request.QueryString["Ord_Idint"];
            orderBLL.UpdateOrderChecked(Int32.Parse(orderId), 2);
            BindData();
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
}
